let currentPage = 1;
let pages;
let params = {
    order: "service_id",
    count: 3,
    page: 1
};

$(() => {


    $.post("/service/checkAll", params, showDetails);


    // 条件查询
    $('#searchButton').click(function () {

        // 可修改option value
        let findByStatus = $('#findByStatus').val();
        console.log(findByStatus);
        let findByOS = $('#findByOS').val();
        console.log(findByOS);
        let findByIP = $('#findByIP').val();
        let findByIdcardNo = $('#findByIdcardNo').val();

        /*
        serviceStatus: findByStatus,
        */
        params = {
            order: "service_id",
            count: 3,
            page: 1,
            osUsername: findByOS,
            unixHost: findByIP,
            idCardNo: findByIdcardNo,
            serviceStatus: findByStatus
        };

        $.post("/service/checkAll", params, showDetails);


    });




    function showDetails(resp) {

        if (resp.message === 'OK') {
            let totalPage = resp.data.totalCount;
            pages = Math.ceil(totalPage / params.count);

            let tBody = $('#tbody');
            tBody.empty();

            for (let i of resp.data.items) {


                switch (i.serviceStatus) {
                    case 0:
                        i.serviceStatus = '暂停';
                        break;
                    case 1:
                        i.serviceStatus = '开通';
                        break;
                    case 2:
                        i.serviceStatus = '删除';
                        break;
                }


                let tr = $('<tr></tr>');
                let td01 = $('<td></td>');

                let detail = $('<a href="/service/service_detail.html" class="btn_modify" title="查看明细"></a>').html(i.serviceId);

                td01.append(detail);

                let td02 = $('<td></td>').html(i.accountId);
                let td03 = $('<td></td>').html(i.account.idcardNo);
                let td04 = $('<td></td>').html(i.account.realName);
                let td05 = $('<td></td>').html(i.osUsername);
                let td06 = $('<td></td>').html(i.serviceStatus);
                let td07 = $('<td></td>').html(i.unixHost);
                let td08 = $('<td></td>');
                //  onmouseover="showDetail(true,this);" onmouseout="showDetail(false,this);"
                let costDetail = $('<a class="summary"></a>').html(i.costInfo.costName);
                let detailInfo = $('<div id="detailInfo" class="detail_info">20小时，24.5 元，超出部分 0.03元/分钟</div>');
                td08.append(costDetail).after(detailInfo);

                let td09 = $('<td></td>');
                let btn_start;
                switch (i.serviceStatus) {
                    case '暂停':
                        btn_start = $('<input type="button" value="启用" class="btn_start" />');
                        break;
                    case '开通':
                        btn_start = $('<input type="button" value="暂停" class="btn_pause" />');
                        break;
                }

                let btn_modify = $('<input type="button" value="修改" class="btn_modify" />');


                let btn_delete = $('<input type="button" value="删除" class="btn_delete" />');
                td09.append(btn_start).append(btn_modify).append(btn_delete);

                let td10 = $('<td style="display: none"></td>').html(i.pauseDate);
                let td11 = $('<td style="display: none"></td>').html(i.closeDate);
                let td12 = $('<td style="display: none"></td>').html(i.costId);
                let td13 = $('<td style="display: none"></td>').html(i.loginPasswd);
                let td14 = $('<td style="display: none"></td>').html(i.account.accountStatus);
                let td15 = $('<td style="display: none"></td>').html(i.costInfo.descr);
                let td16 = $('<td style="display: none"></td>').html(i.costInfo.costType);
                let td17 = $('<td style="display: none"></td>').html(i.createDate);



                tBody.append(tr);
                tr.append(td01).append(td02).append(td03)
                    .append(td04).append(td05).append(td06)
                    .append(td07).append(td08).append(td09)
                    .append(td10).append(td11).append(td12)
                    .append(td13).append(td14).append(td15)
                    .append(td16).append(td17);

            }

            drawPagesCount(pages);

        }


    }


    function drawPagesCount(pages) {
        let ul = $('#page');
        ul.empty();
        for (let i = 1; i <= pages; i++) {
            let li = $('<li style="float: left"></li>');
            let a = $('<a></a>');
            if (currentPage == i) {
                a.addClass('current_page');
            }

            a.attr('href', 'javascript:void(0)');
            a.text(i);
            a.click(function () {
                params.page = i;
                $.get("/service/checkAll", params, showDetails);
                currentPage = i;
            });
            li.append(a);
            ul.append(li);
        }
    }

    // 首页
    $('#home').click(function () {
        currentPage = 1;
        params.page = currentPage;
        $.get("/service/checkAll", params, showDetails);
    });


    // 末页
    $('#end').click(function () {
        currentPage = pages;
        params.page = currentPage;
        $.get("/service/checkAll", params, showDetails);
    });


    // 上一页
    $('#prev').click(function () {
        --currentPage;
        if (currentPage < 1) {
            currentPage = 1;
        }

        params.page = currentPage;
        $.get("/service/checkAll", params, showDetails);
    });

    // 下一页
    $('#next').click(function () {
        ++currentPage;
        if (currentPage > pages) {
            currentPage = pages;
        }
        params.page = currentPage;
        $.get("/service/checkAll", params, showDetails);
    });

    // 传递
    $("body").on("click", ".btn_modify", function () {
        // TODO 补全
        let serviceId = $(this).parents("tr").children().eq(0).text();
        let accountId = $(this).parents("tr").children().eq(1).text();
        let idcardNo = $(this).parents("tr").children().eq(2).text();
        let realName = $(this).parents("tr").children().eq(3).text();
        let osUsername = $(this).parents("tr").children().eq(4).text();
        let serviceStatus = $(this).parents("tr").children().eq(5).text();
        let unixHost = $(this).parents("tr").children().eq(6).text();
        let costName = $(this).parents("tr").children().eq(7).text();
        let pauseDate = $(this).parents("tr").children().eq(9).text();// pauseDate
        let closeDate = $(this).parents("tr").children().eq(10).text();// closeDate
        let costId = $(this).parents("tr").children().eq(11).text();// costId
        let loginPasswd = $(this).parents("tr").children().eq(12).text();// loginPasswd
        let accountStatus = $(this).parents("tr").children().eq(13).text();// account.accountStatus
        let descr = $(this).parents("tr").children().eq(14).text();// costInfo.descr
        let costType = $(this).parents("tr").children().eq(15).text();// costInfo.costType
        let createDate = $(this).parents("tr").children().eq(16).text();// createDate



        window.localStorage.setItem("serviceId", serviceId);
        window.localStorage.setItem("accountId", accountId);
        window.localStorage.setItem("idcardNo", idcardNo);
        window.localStorage.setItem("realName", realName);
        window.localStorage.setItem("osUsername", osUsername);
        window.localStorage.setItem("serviceStatus", serviceStatus);
        window.localStorage.setItem("unixHost", unixHost);
        window.localStorage.setItem("costName", costName);
        window.localStorage.setItem("pauseDate", pauseDate);
        window.localStorage.setItem("closeDate", closeDate);
        window.localStorage.setItem("costId", costId);
        window.localStorage.setItem("loginPasswd", loginPasswd);
        window.localStorage.setItem("accountStatus", accountStatus);
        window.localStorage.setItem("descr", descr);
        window.localStorage.setItem("costType", costType);
        window.localStorage.setItem("createDate", createDate);


        window.location.href = "/service/service_modi.html";
    });


    // 启动
    $("body").on("click", ".btn_start", function () {
        let serviceId = $(this).parents("tr").children().eq(0).text();
        let params = {
            serviceId: serviceId,
            serviceStatus: 1
        };
        let flag = window.confirm("确定要启用此业务吗？业务启用后将不能修改和删除。");
        if (flag) {
            $(this).attr("class", "btn_pause");
            $(this).val("暂停");
            $.post("/service/serviceSwitch", params);

            // NEW 抹去暂停时间
            $.post("/service/pauseTimeCancel", {serviceId: serviceId});

            window.location.reload();
        }

    });

    // 暂停
    $("body").on("click", ".btn_pause", function () {
        let serviceId = $(this).parents("tr").children().eq(0).text();
        let params = {
            serviceId: serviceId,
            serviceStatus: 0
        };
        let flag = window.confirm("确定要暂停此业务吗？");
        if (flag) {
            $(this).attr("class", "btn_start");
            $(this).val("启用");
            $.post("/service/serviceSwitch", params);

            // NEW 记录暂停时间
            $.post("/service/pauseTime", {serviceId: serviceId});

            window.location.reload();
        }

    });

    // 删除
    $("body").on("click", ".btn_delete", function () {

        let serviceId = $(this).parents("tr").children().eq(0).text();
        let params = {
            serviceId: serviceId,
            serviceStatus: 2
        };
        let flag = window.confirm("确定要删除此业务吗？");
        if (flag) {
            $.post("/service/serviceSwitch", params);

            // NEW 记录删除时间
            $.post("/service/deleteTime", {serviceId: serviceId});

            window.location.reload();
        }

    });









});

// // 备用
// function showDetail(flag, a) {
//     // var detailDiv = a.parentNode.getElementsByTagName("div")[0];
//     var detailDiv = $('#detailInfo');
//     if (flag) {
//         // detailDiv.style.display = "block";
//         detailDiv.attr("display", "block");
//     }
//     else
//         // detailDiv.style.display = "none";
//         detailDiv.attr("display", "none");
// }